package com.osmino.contacts.groups;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.osmino.contacts.R;
import com.osmino.contacts.entity.Group;
import com.osmino.core.style.IStyleableView;
import com.osmino.core.style.Style;

public class GroupItem extends LinearLayout implements IStyleableView, Checkable {

	private final boolean DBG = true;
	private final int LAYOUT = R.layout.group_list_item;
	private final View mBackgroundView;
	private final TextView mGroupName;
	private final ImageView mGroupImage;
	private final TextView mContactsCount;
	private static final int PIC_DEF = R.drawable.group_item_ic;
	private boolean mChecked = false;
	private final Drawable mBackground, mBackgroundActive, mBackgroundSelected;

	public GroupItem(Context context) {
		super(context);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(LAYOUT, this);
		mBackgroundView = findViewById(R.id.group_list_item_root_ll);
		mGroupName = (TextView) findViewById(R.id.group_list_item_text);
		mGroupImage = (ImageView) findViewById(R.id.group_list_item_image);
		mContactsCount = (TextView) findViewById(R.id.group_list_item_count_text);
		mBackground = getResources().getDrawable(R.drawable.group_item_bg);
		mBackgroundActive = getResources().getDrawable(R.drawable.group_item_bg_act);
		mBackgroundSelected = getResources().getDrawable(R.drawable.group_item_bg_sel);
		setChecked(false);
	}

	public void clearInfo() {
		mGroupName.setText("");
		mGroupImage.setImageResource(PIC_DEF);
		mContactsCount.setText("" + 0);
	}

	public void setInfo(Group info) {
		clearInfo();
		String title = info.title;
		if (!TextUtils.isEmpty(title)) {
			title = title.replaceAll("(System|Group|:)", "").trim();
		}
		mGroupName.setText(title);
		if(info.getIconResource()!=Group.NO_ICON){
			mGroupImage.setImageResource(info.getIconResource());
		}
		else{
			mGroupImage.setImageResource(Group.ICON_DEFAULT);
		}
		mContactsCount.setText("" + info.sizeOfContactList());
	}

	@Override
	public void setStyle(Style style) {
	}

	@Override
	public boolean isChecked() {
		return mChecked;
	}

	@Override
	public void setChecked(boolean checked) {
		mChecked = checked;
		mBackgroundView.setBackgroundDrawable(mChecked ? mBackgroundActive : mBackground);
	}
	
	@Override
	public void setSelected(boolean selected) {
		mBackgroundView.setBackgroundDrawable(selected ? mBackgroundSelected : mBackground);
	}
	
	@Override
	public void toggle() {
		setChecked(!mChecked);
	}
	
	public void setBelowDND(boolean isBelowDND){
//		System.out.println(isBelowDND);
		mBackgroundView.setBackgroundDrawable(isBelowDND ? mBackgroundSelected : mChecked ? mBackgroundActive : mBackground );
	}
}